<?php
namespace app\api\controller;
use think\App;
use think\Controller;
use think\Db;
use think\Request;

use app\api\validate\CheckValidate;
use app\api\model\TelCode;
use app\api\model\MemberTmp;
use app\api\model\Member;
use app\api\model\Banner;
use app\api\model\Course as CM;
use app\api\model\CourseMenu;
use app\api\model\Lecturer;
use app\api\model\CourseSubject;
use app\api\model\MemberCourseNote;
use app\api\model\CourseRecord;
use app\api\model\CourseMenuRecord;
use app\api\model\MemberCourseQuestion;


class Course extends Common
{
    private $noCheckAction = [];//不需要验证的方法
    
    public function initialize(){
        parent::initialize();
//        $this->doCheckAuth($this->noCheckAction);
    }
   

    /**
    * 列表
    */
    public function lists(){
        $request = $this->request->post();
        $where = ['is_try_see'=>-1];
        if(!empty($request['cate_id'])){
            $cate_id = (int)$request['cate_id'];
            $where['cate_id']= $cate_id;
        }
        $model = new CM;
        
        $list = $model->_list($where);
        die(apiSuccess());
    }


    /**
    * 详情
    */
    public function detail(){
        $request = $this->request->post();
        
        $id = (int)$request['id']; //课程id
        if(empty($id)){
            die(apiError('课程id必须'));
        }
        $member = $this->member;
        $mid = $member['id'];
        $model = new CM;
        $course = $model->_getInfoByID($id);
        
        if(empty($course)){
            die(apiError('课程不存在'));
        }
        $class_id = $course['class_id'];
        $subject = $model->_getList($class_id);
        $model = new CourseMenu;
        $detail = [];
        $list = [];
        $lecturer = [];
        $detail = $model->_getByCid($id);
        
        $start = 0;
        /**
        * 校验是否有记录
        */
        $recordModel = new CourseRecord;
        $record = $recordModel->_getInfoByWhere(['mid'=>$mid, 'cid'=>$id]);
        $scan_time = 0;
        if(!empty($record)){
            $detail = $model->_getInfoByID($record['last_cmid']);
            $scan_time = $record['scan_time'];
            
        }
        $ii = [
            'mid' => $mid,
            'cid' => $id,
            'cmid' => $detail['id'],
        ];
        $ret = $recordModel->_createRecord($ii);
        
        $detail['scan_time'] = $scan_time;
        $where = ['a.cid'=>$id];
        $lid = $course['lid'];
        
//        if(!empty($request['lid'])){
//            $where['a.lid'] = $request['lid'];
//        }else{
//            $where['a.lid'] = $detail['lid'];
//        }

        
        if(!empty($request['version'])){
            $where['a.is_new'] = $request['version'];
        }else{
            $where['a.is_new'] = $detail['is_new'];
        }
        
        $list = $model->_list($where);
        $all = $list['data'];
        foreach ($all as $k=>$v){
            //根据课程寻找班级
            $class_id = Db::name('course')->where('id',$v['cid'])->value('class_id');
            //根据用户和班级查找是否开签协议
            $map['cid'] = $class_id;
            $map['mid'] = $mid;
            $member_class_info = Db::name('member_class')->where($map)->find();
            //已经签协议
            $is_pay_status = 0;
            if($member_class_info['is_sign_agreement']==1){
                $all[$k]['is_pay'] = -1;
                $is_pay_status      = $member_class_info['status'];
            }
            $all[$k]['is_pay_status'] = $is_pay_status;
        }
        $list['data'] = $all;
        $model = new Lecturer;
        $lecturer = $model->_getListByWhere([['id', 'in', $lid]]);
        $data = [
            'info'=>$detail,
            'list'=>$list,
            'lecturer'=>$lecturer,
            'subject'=>$subject,
        ];
        
        die(apiSuccess($data));
    }
    
    /**
    * 记录会员章节信息
    */

    public function addMenuRecord(){
        $request = $this->request->post();
        $member = $this->member;
        $mid = $member['id'];
        $model = new CourseMenuRecord;
        $request['mid'] = $mid;
        $ret = $model->_createMenuRecord($request);
        if(!$ret){
            die(apiError($model->getError()));
        }
        die(apiSuccess([],'成功'));
    }
    
    
    /**
    * 课程笔记列表
    */
    public function courseNoteList(){
        $request = $this->request->post();
        $member = $this->member;
        $mid = $member['id'];
        $model = new MemberCourseNote;
        $request['mid'] = $mid;
        $list = $model->_noteList($request);
        die(apiSuccess($list));
    }
    
    /**
    * 课程问题列表
    */
    public function courseQuestionList(){
        $request = $this->request->post();
        $member = $this->member;
        $mid = $member['id'];
        $model = new MemberCourseQuestion;
        $request['mid'] = $mid;
        $list = $model->_questionList($request);
        die(apiSuccess($list));
    }

    /**
     * 课程问题详情
     */
    public function courseQuestionDetail(){
        $request = $this->request->post();
        $model   = new MemberCourseQuestion;
        $where['mcq.id'] = $request['question_id'];
        $list = $model->_questionDetail($where);
        die(apiSuccess($list));
    }
    
    /**
    * 记录会员浏览信息
    */
    public function recordMemberScan(){
        $model = new CourseRecord;
        $request = $this->request->post();
        $member = $this->member;
        $mid = $member['id'];
        $request['mid'] = $mid;
        $ret = $model->_recordMemberScan($request);
        if(!$ret){
            die(apiError($model->getError()));
        }
        die(apiSuccess('记录成功'));
    }
}
